//
//  THJInvitationDataVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/7/7.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJInvitationDataVC.h"
#import "THJItemTypeView.h"
#import "ZBNetworking.h"
@interface THJInvitationDataVC ()<THJItemTypeViewDelegate>
{
    NSMutableArray * _bigpriceArr;
}
@property (weak, nonatomic) IBOutlet PYZoomEchartsView *chartView;

@property (nonatomic,strong)NSArray * dateArr;
@property (weak, nonatomic) IBOutlet UIButton *itemBtn;
@property (nonatomic,strong)NSMutableArray * nameArr;

@property (nonatomic,strong)NSMutableArray * typeNameArr;
@property (nonatomic,strong)NSMutableArray * typeIdArr;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@end

@implementation THJInvitationDataVC
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setStateBarColor:SingleColor(clearColor)];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLOR(34, 34, 34, 1);
    self.tabBarController.tabBar.hidden = YES;
    self.topSpace.constant = NavbarHeight;
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:self.pageTitle];
    }else{
        [self setNavBarWithTitle:self.pageTitle];
    }
    [self getTypeData];
}
-(NSMutableArray*)typeNameArr{
    if(!_typeNameArr){
        _typeNameArr = [NSMutableArray new];
    }
    return _typeNameArr;
}
-(NSMutableArray*)typeIdArr{
    if(!_typeIdArr){
        _typeIdArr = [NSMutableArray new];
    }
    return _typeIdArr;
}
//类型选择
-(void)getTypeData{
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    __weak typeof(self) weakself = self;
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString = weakself.getTypeUrl;
        NSLog(@"类型-url--%@",request.urlString);
        request.apiType=ZBRequestTypeCache;
    }  success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if([receiveData[@"status"] isEqual:@"success"]){
            for (NSDictionary * dic in receiveData[@"data"]) {
                [weakself.typeNameArr  addObject:dic[@"name"]];
                [weakself.typeIdArr  addObject:dic[@"type"]];
            }
            [weakself.itemBtn setTitle:weakself.typeNameArr[0] forState:UIControlStateNormal];
            [weakself loadDataWithTypeId:weakself.typeIdArr[0]];
        }
    } failed:^(NSError *error){
        
    }];
}
- (IBAction)typeSelectBtn:(id)sender {
    
    THJItemTypeView * view = [[THJItemTypeView alloc] initWithArr:self.typeNameArr];
    view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
//    view.dataArr = self.typeNameArr;
    view.delegate = self;
    view.backgroundColor = COLOR(0, 0, 0, 0.3);
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:view];
}
#pragma mark ---- THJItemTypeViewDelegate
-(void)selectItemIndex:(NSInteger)index{
     [self.itemBtn setTitle:self.typeNameArr[index] forState:UIControlStateNormal];
    [self loadDataWithTypeId:self.typeIdArr[index]];
}
/**
 getdata

 @param type 类型
 */
-(void)loadDataWithTypeId:(NSString*)type{
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    NSString * url = [NSString stringWithFormat:@"%@%@",self.getDataUrl,type];
    __weak typeof(self)weakself = self;
    [THJNetWork getNetDataFromUrl:url callback:^(NSDictionary* receiveData, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        weakself.dateArr = receiveData[@"data"][@"timedata"];
        if([receiveData[@"data"][@"list"] isKindOfClass:[NSArray class]]){
            NSArray * arr = receiveData[@"data"][@"list"];
            _bigpriceArr = [NSMutableArray array];
            for (NSDictionary * dic in arr ) {
                NSMutableArray * priceArr = [NSMutableArray array];
                for (NSString * price in dic[@"dataList"]) {
                    if ([price floatValue] == 0) {
                        [priceArr addObject:[NSNull null]];
                    }else{
                        [priceArr addObject:[NSNumber numberWithFloat:[price floatValue]]];
                    }
                }
                [_bigpriceArr addObject:priceArr];
            }
            _nameArr =  receiveData[@"data"][@"typeList"];
            [self setChartViewData];
        }else if([receiveData[@"data"][@"list"] isKindOfClass:[NSDictionary class]]){
            NSDictionary * listDic = receiveData[@"data"][@"list"];
            _bigpriceArr = [NSMutableArray array];
           
            NSMutableArray * priceArr = [NSMutableArray array];
            for (NSString * price in listDic[@"dataList"]) {
                if ([price floatValue] == 0) {
                    [priceArr addObject:[NSNull null]];
                }else{
                    [priceArr addObject:[NSNumber numberWithFloat:[price floatValue]]];
                }
            }
            [_bigpriceArr addObject:priceArr];
            
            _nameArr =  receiveData[@"data"][@"typeList"];
            [self setChartViewData];
        }
       
    }];
}
-(void)setChartViewData {
    [_chartView setOption:[self standardLineOptionWithArr1:_bigpriceArr DateArr:self.dateArr]];
    [_chartView loadEcharts];
}
- (PYOption *)standardLineOptionWithArr1:(NSArray*)arr1 DateArr:(NSArray*)dateArr{
    
    return [PYOption initPYOptionWithBlock:^(PYOption *option) {
        option
        .tooltipEqual([PYTooltip initPYTooltipWithBlock:^(PYTooltip *tooltip) {
            tooltip.triggerEqual(PYTooltipTriggerAxis);
        }])
        .legendEqual([PYLegend initPYLegendWithBlock:^(PYLegend *legend) {
            legend.dataEqual(_nameArr);
            legend.textStyle.colorEqual(@"#ffffff");
        }])
        .gridEqual([PYGrid initPYGridWithBlock:^(PYGrid *grid) {
            grid.xEqual(@45).x2Equal(@20);
            grid.yEqual(@80).y2Equal(@30);
        }])
        .addXAxis([PYAxis initPYAxisWithBlock:^(PYAxis *axis) {
            axis.typeEqual(PYAxisTypeCategory)
            .boundaryGapEqual(@NO)
            .addDataArr(dateArr);
            axis.axisLabel.textStyleEqual([PYTextStyle initPYTextStyleWithBlock:^(PYTextStyle *textStyle) {
                textStyle.colorEqual(@"#ffffff");
            }]);
        }])
        .addYAxis([PYAxis initPYAxisWithBlock:^(PYAxis *axis) {
            axis.typeEqual(PYAxisTypeValue);
            axis.axisLabel.textStyleEqual([PYTextStyle initPYTextStyleWithBlock:^(PYTextStyle *textStyle) {
                textStyle.colorEqual(@"#ffffff");
            }]);
        }]);
        for (NSInteger i = 0;i< arr1.count;i++) {
            option.addSeries([PYSeries initPYSeriesWithBlock:^(PYSeries *series) {
                series.nameEqual(_nameArr[i])
                .typeEqual(PYSeriesTypeLine)
                .dataEqual(arr1[i]);
            }]);
        }
        
        
    }];
}

@end
